package hks.saigyoujiyuyuko.bcverification.Util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.text.DecimalFormat;
import java.util.Random;

import hks.saigyoujiyuyuko.bcverification.Var.Var;

public class GeneralUtil implements Util{
	
	/**
	 * @author SaigyoujiYuyuko
	 * 
	 * @param file	待写入的文件对象
	 * @param content	写入的内容
	 * 
	 * @usage 写入文件
	 */
	
	@Override
	public void WriteFile(File file, String content)throws IOException {
		String FileContent = "";
		if (file.exists()) {
			FileContent = Var.G_UTIL.ReadFile(file);
		}

		BufferedWriter bWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
		
		bWriter.write(FileContent + content);
		
		bWriter.close();
	}

	
	/**
	 * @author SaigyoujiYuyuko
	 * 
	 * @param file	文件对象
	 * @param key	获取的key
	 * 
	 * @usage 获取键值
	 */
	
	@Override
	public String GetFileKey(File file, String key) throws IOException{
		BufferedReader bReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
		
	    for (String text = ""; (text = bReader.readLine()) != null;) {
	    	
	      if (text.toLowerCase().contains(key.toLowerCase())){
	    	  bReader.close();
	         
	        return text.replace(key, "");
	      }
	  
	    }
	    
	    bReader.close();
		return "null";
	}
	
	
	/**
	 * @author SaigyoujiYuyuko
	 * 
	 * @param file	文件对象
	 * @param key	获取的key
	 * 
	 * @usage 读取文件全部内容
	 * 
	 * @return String 获取的内容
	 */
	
	@Override
	public String ReadFile(File file)throws IOException {
		BufferedReader bReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
		
		String content = "";
	    for (String text = ""; (text = bReader.readLine()) != null;) {
	    	content = content + text + "\n";
	    }
	    
	    bReader.close();
		return content;
	}

	
	/**
	 * @author SaigyoujiYuyuko
	 * 
	 * @param min	 最小
	 * @param max 最大
	 * 
	 * @usage 生成一个随机数
	 * 
	 * @return Int 生成的随机数
	 */
	
	@Override
	public int Rand(int min, int max) {
		DecimalFormat decimalFormat = new DecimalFormat("#");
		int middleNum = Integer.valueOf(decimalFormat.format(max/min));
		
		 Random rand = new Random();
		 int randNum = rand.nextInt(max);
		 
		 if (randNum < middleNum) {
			 randNum++;
			 if (randNum > max) {
				 randNum = randNum - 2;
			}
		}else {
			randNum--;
			 if (randNum < min) {
				 randNum = randNum + 2;
			}
		}
		 
		return randNum;
	}

	
	/**
	 * @author SaigyoujiYuyuko
	 * 
	 * @param key	获取的key
	 * 
	 * @usage 获取字符串的键值
	 */
	
	@Override
	public String GetStringKey(String inPutSting) throws IOException {
		 if (inPutSting.toLowerCase().contains("[cmd]")){
	        return "cmd" + inPutSting.replace("[cmd]", "");
	     }
		 
		 if (inPutSting.toLowerCase().contains("[stop]")){
			 return "stp" + inPutSting.replace("[stop]", "");
		 }
		
		 return "none";
	}

}
